<?php

namespace App\Controller\Api\User;

use App\Constants\ErrorCode;
use App\Controller\Api\BaseController;
use App\Model\ChatUser;
use App\Model\ConsultantDatum;
use App\Model\OrderReview;
use App\Model\UserFollow;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Hyperf\HttpServer\Annotation\RequestMapping;


/**
 * @Middleware(JWTAuthMiddleware::class)
 * @Controller(prefix="/user/v1/consultant")
 */
class ConsultantController extends BaseController
{


    /**
     * 咨询师列表
     * @RequestMapping(path="consultantList")
     */
    public function consultantList()
    {

        //todo 前期就只把需要字段整理好返回 开发完成后 进行算法推荐


        $list = ConsultantDatum::getConsultantList();

        return $this->response->returnSuccess(['list' => $list]);


    }


    /**
     * 咨询师详情
     * @RequestMapping(path="consultantDetails")
     */
    public function consultantDetails()
    {

        $uid = $this->request->query('id');

        //可缓存数据 个人相册  名称 国家 认证信息 角色标签  行业经验  评论（3条）  学历证书信息 咨询师语言

        $cacheKey = 'consultantBaseInfo' . $uid;
        $consultantBaseInfo = cache()->get($cacheKey);
        if (is_null($consultantBaseInfo)) {
            $consultantBaseInfo = ConsultantDatum::consultantBaseInfo($uid);

            if (is_null($consultantBaseInfo)) {
                return $this->response->returnFail(ErrorCode::PARAMS_FALSE);
            } else {
                cache()->set($cacheKey, $consultantBaseInfo);
            }

        }

        $online = ChatUser::where(['uid' => $uid])->select(['online', 'service_status','username'])->first();

        $consultantBaseInfo['online'] = $online['online'] ?? 0;
        $consultantBaseInfo['serviceStatus'] = $online['service_status'] ?? 0;
        $consultantBaseInfo['num'] = $online['username'] ?? 0;


        $follow = UserFollow::where(['uid'=>$this->uid(),'aid'=>$uid])->value('follow');
        $consultantBaseInfo['follow'] = $follow?$follow:0;


        $favorableList = OrderReview::getFavorableList($uid);
        $consultantBaseInfo['favorableList'] = $favorableList;


        return $this->response->returnSuccess(['info' => $consultantBaseInfo]);


    }


    /**
     * 咨询师评价列表
     * @RequestMapping(path="evaluateList")
     */
    public function evaluateList()
    {

        $uid = $this->request->query('id', 0);


        $list = OrderReview::getEvaluateList($uid);

        return $this->response->returnSuccess(['list' => $list]);


    }


}
